/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.component.redis;

import com.workcell.workdata.im.component.redis.base.LongRedisTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MomentListRedisTemplate extends LongRedisTemplate {

    private static final String KEY_ME_BLOCK_LIST = "MOMENT_RULE_ME_%d";
    private static final String KEY_ME_IGNORE_LIST = "MOMENT_RULE_MI_%d";
    private static final String KEY_BLOCK_ME_LIST = "MOMENT_RULE_BM_%d";
    private static final String KEY_IGNORE_ME_LIST = "MOMENT_RULE_IM_%d";

    public MomentListRedisTemplate(RedisConnectionFactory connectionFactory) {
        super(connectionFactory);
    }

    public void block(long uid, long fid, boolean add) {
        this.handle(String.format(KEY_ME_BLOCK_LIST, uid), fid, add);
        this.handle(String.format(KEY_BLOCK_ME_LIST, fid), uid, add);
    }

    public void ignore(long uid, long fid, boolean add) {
        this.handle(String.format(KEY_ME_IGNORE_LIST, uid), fid, add);
        this.handle(String.format(KEY_IGNORE_ME_LIST, fid), uid, add);
    }


    private void handle(String key, long fid, boolean add) {
        BoundSetOperations<String, Long> operations = super.boundSetOps(key);
        if (add) {
            operations.add(fid);
        } else {
            operations.remove(fid);
        }
    }

    public void addMeBlockList(long uid,List<Long> idList) {
        super.add(String.format(KEY_ME_BLOCK_LIST, uid),idList);
    }

    public void addMeIgnoreList(long uid,List<Long> idList) {
        super.add(String.format(KEY_ME_IGNORE_LIST, uid),idList);
    }

    public void addBlockMeList(long uid,List<Long> idList) {
        super.add(String.format(KEY_BLOCK_ME_LIST, uid),idList);
    }

    public void addIgnoreMeList(long uid,List<Long> idList) {
        super.add(String.format(KEY_IGNORE_ME_LIST, uid),idList);
    }

    public List<Long> findBlockMeList(long uid) {
        return findList(String.format(KEY_BLOCK_ME_LIST, uid));
    }

    public List<Long> findIgnoreMeList(long uid) {
        return findList(String.format(KEY_IGNORE_ME_LIST, uid));
    }

    public List<Long> findMeBlockList(long uid) {
        return findList(String.format(KEY_ME_BLOCK_LIST, uid));
    }

    public List<Long> findMeIgnoreList(long uid) {
        return findList(String.format(KEY_ME_IGNORE_LIST, uid));
    }

}
